package controller.member;

import databaseLayer.Member_db;
import databaseLayer.MovieSection_db;
import databaseLayer.Purchase_db;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.MovieSection;
import model.Purchase;

public class MemverViewUnpaid extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String action = request.getParameter("action");

            if (action == null) {
                doDefault(request, response);
            } else if (action.equals("payment")) {
                doPayment(request, response);
            } else {
                doDefault(request, response);
            }
        } catch (Exception e) {
            HttpSession session = request.getSession();
            session.setAttribute("errorMessage", e.getMessage());
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/member/viewUnpaid.jsp");
            requestDispatcher.forward(request, response);
        }
    }

    private void doPayment(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, Exception {
        HttpSession session = request.getSession();

        // prepare for the payment

        String movieSectionId = request.getParameter("movieSectionId");
        MovieSection target = MovieSection_db.selectMovieSectionById(movieSectionId);
        session.setAttribute("targetMovieSection", target);

        String memberUsername = request.getRemoteUser();
        session.setAttribute("memberUsername", memberUsername);

        String seatNo = request.getParameter("seatNo");
        String[] arrSeats = {seatNo};
        session.setAttribute("arrSeats", arrSeats);

        session.setAttribute("numTickets", Integer.toString(1));

        int singleTicketPrice = target.getPrice();
        Integer totalPayment = singleTicketPrice * arrSeats.length;
        session.setAttribute("totalPayment", totalPayment);

        Integer loyaltyPoint = Member_db.getInstance().getLoyaltyPoint(memberUsername);
        session.setAttribute("loyaltyPoint", loyaltyPoint);

        // forward to payment
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/member/payment.jsp");
        requestDispatcher.forward(request, response);
    }

    private void doDefault(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, Exception {
        HttpSession session = request.getSession();

        String memberUsername = request.getRemoteUser();
        List<Purchase> listPurchase = Purchase_db.getInstance().selectUnpaidByUsername(memberUsername);

        List<MovieSection> listMovieSection = new ArrayList();
        for (Purchase curr_purchase : listPurchase) {
            String id = curr_purchase.getPurchaseId().getMovieSectionId();
            listMovieSection.add(MovieSection_db.selectMovieSectionById(id));
        }

        session.setAttribute("listPurchase", listPurchase);
        session.setAttribute("listMovieSection", listMovieSection);
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/member/viewUnpaid.jsp");
        requestDispatcher.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
